package com.business.weixin.util;

import com.business.util.HttpUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinTempleteMsg {
    @Autowired
    WeixinService weixinService;
    /**
     * 发送模版消息
     * 流程发起时通知
     * 所需信息 openid 发起人 当前环节 流程名称 流程实例id
     {{first.DATA}}
     流程名称：{{keyword1.DATA}}
     发起人：{{keyword2.DATA}}
     当前环节：{{keyword3.DATA}}
     {{remark.DATA}}
     */
    public void sendTemplete_01(String openid,String createUserName,String taskName,String appealTypeName,String processInstanceId){
        String template_id="I8lQBSpyU5b9o27ycHwao7zeJ9vdyWcTGCHG3i4m5vU";
        String url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+weixinService.getAccessToken();
        WxTemplate temp = new WxTemplate();
        temp.setUrl("https://xzsbdc.zzgfdz.cn/act/getProccessActiveNodeImage?processInstanceId="+processInstanceId);
        temp.setTouser(openid);
        temp.setTopcolor("#000000");

        temp.setTemplate_id(template_id);//模版id
        Map<String,TemplateData> m = new HashMap<String,TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue(createUserName+"发起"+appealTypeName);
        m.put("first", first);

        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(appealTypeName);
        m.put("keyword1", keyword1);


        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(createUserName);
        m.put("keyword2", keyword2);

        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#000000");
        keyword3.setValue(taskName);
        m.put("keyword3", keyword3);

        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("您有任务需要处理，请登录后台进行处理");
        m.put("remark", remark);
        temp.setData(m);

        String jsonString = JSONObject.fromObject(temp).toString();
        System.out.println("模版消息：");
        System.out.println(jsonString);
        JSONObject jsonObject = HttpUtil.httpRequest(url, "POST", jsonString);
        System.out.println(jsonObject);
        int result = 0;
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                result = jsonObject.getInt("errcode");
            }
        }
    }
}
